package com.neulogistics.contrib.procuredcenter.api.application.validator;

import cn.hutool.core.util.StrUtil;
import com.neulogistics.contrib.procuredcenter.api.application.validator.annotation.ValidPurchaseItem;
import com.neulogistics.contrib.procuredcenter.domain.model.valueobject.PurchaseItem;


import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PurchaseItemValidator implements ConstraintValidator<ValidPurchaseItem, PurchaseItem> {
    @Override
    public void initialize(ValidPurchaseItem constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(PurchaseItem item, ConstraintValidatorContext constraintValidatorContext) {
        // 为空则放行
        if(item == null)
            return true;

        // 不为空，则所有元素都不能为空,且输入要合法
        if(item.getGoodsId() == null || item.getGoodsId() <= 0)
            return false;

        if(StrUtil.isBlank(item.getGoodsName()))
            return false;

        if(item.getGoodsPrice() == null || item.getGoodsPrice() <= 0)
            return false;

        if(item.getCount() == null || item.getCount() <= 0)
            return false;

        if(StrUtil.isBlank(item.getMeasureUnit()))
            return false;

        return true;
    }
}
